import Cookies from 'js-cookie'
/*
localStorage:永久存储  getItem  setItem   removeItem
sessionStorage：临时存储，窗口会话级别存储  getItem  setItem   removeItem
cookie：有大小限制，个数限制，主动参与接口请求
js-cookie:作用，提供了cookie操作的api
  安装与导入该插件
  yarn add js-cookie
  import Cookies from 'js-cookie'
    获取:Cookies.get(key)
    设置:Cookies.set(key,value)
    删除:Cookies.remove(key)
持久化存储了菜单的状态（展开与收起的状态）
    a:修改了相应的值就存储到浏览器
    b:初始值读取浏览器的存储
*/

const state = {
  sidebar: {
    // 菜单的展开与收起做了持久化，默认是展开
    // boolean?真的结果:假的结果
    opened: Cookies.get('sidebarStatus')
      ? !!+Cookies.get('sidebarStatus')
      : true,
    // 没有动画:false 代表有动画
    withoutAnimation: false
  },
  device: 'desktop'
}

const mutations = {
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
